package user

import (
	"crypto/md5"
	"fmt"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

// 加密密码
func EncryptPassword(password, salt string) string {
	return fmt.Sprintf("%x", md5.Sum([]byte(password+salt)))
}

// 随机生成密码
func RandPassword() string {
	rand.Seed(time.Now().UnixNano())
	return strings.TrimPrefix(strconv.Itoa(1000000+rand.Intn(1000000)), "1")
}

// 随机生成盐值
func RandSlat() string {
	rand.Seed(time.Now().UnixNano())
	return strings.TrimPrefix(strconv.Itoa(1000000+rand.Intn(1000000)), "1")
}

// 随机生成密码
func RandCaptcha() string {
	rand.Seed(time.Now().UnixNano())
	return strings.TrimPrefix(strconv.Itoa(1000000+rand.Intn(1000000)), "1")
}

// 根据出生时间获取年龄
func Age(v int64) int {
	if v == 0 {
		return 0
	}
	now := time.Now()
	birth := time.Unix(v, 0)
	age := now.Year() - birth.Year()
	if now.Unix() < birth.AddDate(age, 0, 0).Unix() {
		age--
	}
	return age
}

func Sex(v int) string {
	switch v {
	case 1:
		return "男"
	case 2:
		return "女"
	default:
		return "未设置"
	}
}